/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package lib.model;

import lib.xml.XMLizable;
import lib.model.distribution.Distribution;
import java.util.ArrayList;
import java.util.List;

/**
 * This class represents a task that should be done to get an item done, the
 * task has a length determinated by a probability distribution. The task must
 * be performed in a single station, but it may be more than one station capable
 * to perform the task. The task could be forced to be performed in a single
 * shift or it could be done independently of how many shifth need to be
 * completed.
 * @see Distribution
 * @see Station
 * @author Jorge Mira Yagüe
 */
public class Task implements XMLizable {

    private String name;
    private String description;
    private List<String> stations;
    private Distribution distribution;
    private boolean sameShift;

    /**
     * Builds an empty task
     */
    public Task() {
        name = "";
        description = "";
        stations = new ArrayList<String>();
        distribution = null;
        sameShift = false;
    }

    /**
     * Bulids a task from the given parameters
     * @param name the name of the task
     * @param distribution the probability distribution which determines the
     * task length
     * @param description the description of the task
     * @param sameShift indicates if the task must be done in one shift
     * @see Distribution
     */
    public Task(String name,
            Distribution distribution,
            String description,
            boolean sameShift) {
        this.name = name;
        this.distribution = distribution;
        this.description = description;
        stations = new ArrayList<String>();
        this.sameShift = sameShift;
    }

    /**
     * Adds a station capable to do the task
     * @param station the name of the station to be added
     * @return true (as specified in Collecion.add)
     */
    public boolean addStation(String station) {
        return stations.add(station);
    }

    /**
     * Removes a station from the task
     * @param station the name of the station to be removed
     * @return true if the task contained the station
     */
    public boolean removeStation(String station) {
        return stations.remove(station);
    }

    public String getStation(int index) {
        return stations.get(index);
    }

    public List<String> getStations() {
        return stations;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setDistribution(Distribution distribution) {
        this.distribution = distribution;
    }

    public String getName() {
        return name;
    }

    public Distribution getDistribution() {
        return distribution;
    }

    public double getTime() {
        return distribution.getResult();
    }

    public boolean getSameShift() {
        return sameShift;
    }

    public String toXML() {

        String result = "<Task ";
        result += "name=\"" + name + "\" ";
        result += "description=\"" + description + "\" ";
        result += "sameShift=\"" + sameShift + "\" ";
        result += ">\n";
        result += distribution.toXML();
        if (stations != null) {
            for (String stationName : stations) {
                result += "<stationName name=\"" + stationName + "\"/>\n";
            }
        }

        result += "</Task>\n";
        return result;
    }
}
